package org.anderground.modules.editor.importers.pmz.archive;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class EntrySection {
	private String name;
	private static final boolean ignoreComments = false;
	List<PmzEntryRecord> records = new ArrayList<PmzEntryRecord>();
	
	EntrySection(String name, ForetellerReader reader) {
		this.name = name.substring(1,name.length()-1);		
		//ForetellerReader r = new ForetellerReader(reader);
		getRecords(reader);
	}
	
	public String[] getValues(String name) {
		for(PmzEntryRecord record: records) {
			
			if(name.equals(record.getName())) {
				//System.out.println(name + "=" + record.getValues()[0]);
				return record.getValues();
			}
		}
		return null;
	}
	public PmzEntryRecord[] getRecords() {
		return records.toArray(new PmzEntryRecord[0]);
	}
	private void getRecords(ForetellerReader reader) {
		
		PmzEntryRecord record = readEntry(reader);
		do {
			//don't add commented strings
			if(record!=null && !ignoreComments && record.getName().charAt(0)!=';'){
				records.add(record);
			}
			record = readEntry(reader);
		}
		while(record!=null);
	}

	/**
	 * @param reader
	 * @return null if there is no records to read left
	 */
	private PmzEntryRecord readEntry(ForetellerReader reader) {
		try {
			String str = reader.readLine();
			if(str==null || str.equals("")) 
				if(reader.spyNextLine()!=null && !reader.spyNextLine().contains("[")) {
					while(reader.spyNextLine()=="") {
						if(reader.spyNextLine().contains("[")) return null;
						reader.readLine();
					}
					str=reader.readLine();
				}
				else return null;
			
			String[] temp = str.split("=");
			if(temp.length>1)
				
				return new PmzEntryRecord(temp[0],checkQuotes(temp[1].split(",")));
			else
				return new PmzEntryRecord(temp[0],new String[0]);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
		
	}


	private String[] checkQuotes(String[] split) {
		List<String> result = new ArrayList<String>();
		String str;
		for (int i = 0; i < split.length; i++) {
			str = split[i];
			if (split[i].indexOf('\"') != -1) {
				do {
					i++;
					str += "," + split[i];
					
				} while (split[i].indexOf('\"') == -1);
			}
			result.add(str);
		}
		return result.toArray(new String[0]);
	}

	public String getName() {
		return name;
	}
	
	
	public PmzEntryRecord[] getEntryRecords() {
		return records.toArray(new PmzEntryRecord[0]);
		
	}
	
	public String toString() {
		String section = '[' + name + ']' + '\n'; 
		for(PmzEntryRecord record: getEntryRecords()) {
			section+=record.getName() + '=';
			for(String value:record.getValues()) {
				section+= value + ',';
			}
			section+='\n';
		}
		return  section;
	}
	
}

class ForetellerReader extends BufferedReader {
	String nextLine;
	public ForetellerReader(Reader in) {
		super(in);
		try {
			nextLine = super.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public String readLine() throws IOException {
		String temp = nextLine;
		if(super.ready()) nextLine = super.readLine();
		else nextLine=null;
		return temp;
	}
	
	@Override
	public boolean ready() {
		return nextLine==null;
	}
	
	public String spyNextLine() {
		return nextLine;
	}
	
	
}
